package com.example.propertymanagement.service;

import com.example.propertymanagement.dto.BuildingDTO;
import com.example.propertymanagement.entity.Building;
import com.example.propertymanagement.repository.BuildingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * BuildingService：楼栋的业务逻辑处理
 */
@Service
public class BuildingService {

    @Autowired
    private BuildingRepository buildingRepository;  // 注入 BuildingRepository

    /**
     * 获取所有楼栋
     * @return 返回所有楼栋列表
     */
    public List<BuildingDTO> getAllBuildings() {
        List<Building> buildings = buildingRepository.findAll();  // 获取所有楼栋
        return buildings.stream()
                .map(this::convertToDTO)  // 将 Building 转换为 BuildingDTO
                .collect(Collectors.toList());
    }

    /**
     * 根据小区ID获取楼栋列表
     * @param communityId 小区ID
     * @return 返回指定小区的所有楼栋
     */
    public List<BuildingDTO> getBuildingsByCommunity(Integer communityId) {
        List<Building> buildings = buildingRepository.findByCommunityId(communityId);  // 获取小区下所有楼栋
        return buildings.stream()
                .map(this::convertToDTO)  // 将 Building 转换为 BuildingDTO
                .collect(Collectors.toList());
    }

    /**
     * 根据楼栋ID获取楼栋信息
     * @param id 楼栋ID
     * @return 返回楼栋DTO
     */
    public Optional<BuildingDTO> getBuildingById(Integer id) {
        return buildingRepository.findById(id).map(this::convertToDTO);  // 根据楼栋ID查询楼栋并转换为DTO
    }

    /**
     * 将 Building 实体转换为 BuildingDTO
     * @param building Building 实体
     * @return 返回转换后的 BuildingDTO
     */
    private BuildingDTO convertToDTO(Building building) {
        BuildingDTO dto = new BuildingDTO();
        dto.setId(building.getId());
        dto.setBuildingCode(building.getBuildingCode());
        dto.setBuildingName(building.getBuildingName());
        dto.setTotalHouseholds(building.getTotalHouseholds());
        dto.setDescription(building.getDescription());
        dto.setCreateTime(building.getCreateTime());
        dto.setOperator(building.getOperator());
        dto.setCommunityId(building.getCommunity().getId());  // 关联小区ID
        dto.setCommunityName(building.getCommunity().getCommunityName());  // 可选：显示小区名称
        return dto;
    }

    /**
     * 创建新的楼栋
     * @param building 楼栋对象，包含要保存的楼栋信息
     * @return 返回保存后的楼栋对象
     */
    public BuildingDTO createBuilding(Building building) {
        building = buildingRepository.save(building);  // 保存楼栋信息
        return convertToDTO(building);  // 转换并返回DTO
    }

    /**
     * 更新楼栋信息
     * @param building 要更新的楼栋对象
     * @return 返回更新后的楼栋DTO
     */
    public BuildingDTO updateBuilding(Building building) {
        building = buildingRepository.save(building);  // 更新楼栋信息
        return convertToDTO(building);  // 转换并返回DTO
    }

    /**
     * 删除指定ID的楼栋
     * @param id 楼栋ID
     */
    public void deleteBuilding(Integer id) {
        buildingRepository.deleteById(id);  // 根据ID删除楼栋
    }
}